package com.sinoglobal.sinostore.activity;

import java.util.ArrayList;

import andbase.listener.AbOnListViewListener;
import andbase.view.pullview.AbPullListView;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.LogUtils;
import com.sinoglobal.sinostore.BaseActivity;
import com.sinoglobal.sinostore.R;
import com.sinoglobal.sinostore.StateBaseActivity;
import com.sinoglobal.sinostore.adapter.CollectionAdapter;
import com.sinoglobal.sinostore.bean.BaseVo;
import com.sinoglobal.sinostore.bean.CollectionListVo;
import com.sinoglobal.sinostore.bean.CollectionVo;
import com.sinoglobal.sinostore.bean.OrderListVo;
import com.sinoglobal.sinostore.dialog.SweetAlertDialog;
import com.sinoglobal.sinostore.dialog.SweetAlertDialog.OnSweetClickListener;
import com.sinoglobal.sinostore.system.Constants;
import com.sinoglobal.sinostore.utils.ApiDebugUtil;
import com.sinoglobal.sinostore.utils.TextUtil;

@SuppressLint("InlinedApi")
public class MyCollectionActivity extends StateBaseActivity implements
		AbOnListViewListener, OnClickListener {

	AbPullListView lvMyCollections;
	
	View noCollectionView;
	
	TextView tvShowPop;

	RadioGroup rgCollectionType;

	View btnGotoMall;

	CollectionAdapter collectionAdapter;
	HttpUtils http = new HttpUtils();
	int pageNum = 1;
	ProgressDialog progressDialog;//
	boolean isShowDialog = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shop_activity_my_collection);
		titleView.setText("我的收藏");
		openChenJInShi();
		init();
		progressDialog = new ProgressDialog(this);
		onLoadMore();
	}

	public void clickPop(View view) {

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return super.onTouchEvent(event);
	}

	private void init() {
		lvMyCollections = getViewById(R.id.lvMyCollections);
		noCollectionView = getViewById(R.id.noCollections);
		rgCollectionType = getViewById(R.id.rgCollectionType);
		btnGotoMall = getViewById(R.id.btnGoToMall);

		btnGotoMall.setOnClickListener(this);
		collectionAdapter = new CollectionAdapter(this,
				new ArrayList<CollectionVo>());
		lvMyCollections.setAdapter(collectionAdapter);
		lvMyCollections.setEmptyView(noCollectionView);
		noCollectionView.setVisibility(View.GONE);
		lvMyCollections.setAbOnListViewListener(this);
		lvMyCollections.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Bundle bundle = new Bundle();
				if (position > 0 && position < collectionAdapter.getCount() + 1) {
					bundle.putString("goodsId",
							collectionAdapter.getItem(position - 1).getObj_id());
				}
				toActivity(GoodsDetailActivity.class, bundle);
			}
		});
		lvMyCollections
				.setOnItemLongClickListener(new OnItemLongClickListener() {

					@Override
					public boolean onItemLongClick(AdapterView<?> parent,
							View view, final int position, long id) {
						if (position > 0
								&& position < collectionAdapter.getCount() + 1) {
							new SweetAlertDialog(MyCollectionActivity.this)
									.setTitleText("确定要删除该收藏吗")
									.setConfirmText("确定")
									.setCancelText("取消")
									.showCancelButton(true)
									.setConfirmClickListener(
											new OnSweetClickListener() {

												@Override
												public void onClick(
														SweetAlertDialog sweetAlertDialog) {
													sweetAlertDialog.dismiss();
													deleteCollection(collectionAdapter
															.getItem(position - 1));
												}
											}).show();
						}

						return true;
					}
				});
		rgCollectionType.check(R.id.rbCollectionAll);
	}

	@Override
	public void onRefresh() {
		pageNum = 1;
		onLoadMore();

	}

	private void deleteCollection(final CollectionVo collectionVo) {
		HttpUtils httpUtils = new HttpUtils();
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("por", "507");
		params.addQueryStringParameter("proIden", Constants.STOREFLAG);
		params.addQueryStringParameter("user_id", Constants.userCenterId);
		params.addQueryStringParameter("id", collectionVo.getId());
		params.addQueryStringParameter("obj_id", collectionVo.getObj_id());
		ApiDebugUtil.debug(params);
		httpUtils.send(HttpRequest.HttpMethod.POST, Constants.STORE_URL,
				params, new RequestCallBack<String>() {

					@Override
					public void onStart() {
						progressDialog.show();
						// resultText.setText("conn...");
					}

					@Override
					public void onLoading(long total, long current,
							boolean isUploading) {
						// resultText.setText(current + "/" + total);
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						progressDialog.dismiss();
						if (responseInfo != null
								&& TextUtil
										.stringIsNotNull(responseInfo.result)) {
							LogUtils.e("responseInfo.result==>"
									+ responseInfo.result);
							BaseVo baseVo;
							try {
								baseVo = JSON.parseObject(responseInfo.result,
										OrderListVo.class);
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
								showShortToast("数据解析错误");
								return;
							}
							LogUtils.e("code==" + baseVo.getCode());
							Toast.makeText(MyCollectionActivity.this,baseVo.getMessage(), 0).show();
							if (!"0".equals(baseVo.getCode())) {
								return;
							}
							collectionAdapter.removeItem(collectionVo);
							pageNum = 1;
							onLoadMore();
						}
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						progressDialog.dismiss();
					}
				});

	}

	@Override
	public void onLoadMore() {
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("por", "506");
		params.addQueryStringParameter("proIden", Constants.STOREFLAG);
		params.addBodyParameter("user_id", Constants.userCenterId);
		params.addBodyParameter("pageSize", "10");
		params.addBodyParameter("page", String.valueOf(pageNum));
		http.send(HttpRequest.HttpMethod.POST, Constants.STORE_URL, params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						if (isShowDialog) {
							progressDialog.show();
						}
						isShowDialog = false;
					}

					@Override
					public void onLoading(long total, long current,
							boolean isUploading) {
					}

					private void loadComplete() {
						progressDialog.dismiss();
						lvMyCollections.stopLoadMore();
						lvMyCollections.stopRefresh();
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						loadComplete();
						if (responseInfo != null
								&& TextUtil
										.stringIsNotNull(responseInfo.result)) {
							LogUtils.e("responseInfo.result==>"+ responseInfo.result);
							CollectionListVo collectionListVo = JSON.parseObject(responseInfo.result,CollectionListVo.class);
							if (collectionListVo != null) {
								LogUtils.e("code=="+ collectionListVo.getCode());

								if (!"0".equals(collectionListVo.getCode())) {
									Toast.makeText(MyCollectionActivity.this,collectionListVo.getMessage(), 0)
											.show();
									lvMyCollections.setPullLoadEnable(false);
									return;
								}
								noCollectionView.setVisibility(View.VISIBLE);
								if (pageNum == 1) {
									collectionAdapter.clearData();
								}
								collectionAdapter.setImgHost(collectionListVo.getHost());
								collectionAdapter.addData(collectionListVo.getList());

								pageNum++;
								int totalPage = 0;
								try {
									totalPage = Integer
											.parseInt(collectionListVo
													.getPageNums());
								} catch (NumberFormatException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
								if (pageNum > totalPage) {
									lvMyCollections.setPullLoadEnable(false);
								} else {
									lvMyCollections.setPullLoadEnable(true);
								}
							}
						}
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						loadComplete();
					}
				});
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.btnGoToMall) {
			toActivity(FirstActivity.class, null);
		} else if (id == R.id.popCollection) {
			clickPop(v);
		}
	}

}
